<!-- partnerDialog -->
<template>
  <el-dialog :title="title" :visible="partnerVisible" @close="close">
    <el-form ref="partnerRef" :model="partnerForm" :rules="formRules">
      <el-form-item
        label="合作商名称"
        :label-width="formLabelWidth"
        prop="name"
      >
        <el-input
          v-model="partnerForm.name"
          autocomplete="off"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        label="联系人"
        :label-width="formLabelWidth"
        prop="contact"
      >
        <el-input
          v-model="partnerForm.contact"
          autocomplete="off"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        label="联系电话"
        :label-width="formLabelWidth"
        prop="mobile"
      >
        <el-input
          v-model="partnerForm.mobile"
          autocomplete="off"
          placeholder="请输入"
          maxlength="11"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        label="分成比例(%)"
        :label-width="formLabelWidth"
        prop="ratio"
      >
        <el-input-number v-model="partnerForm.ratio" placeholder="请输入" controls-position="right" :min="0" :max="100" style="width: 100%" />
      </el-form-item>
      <el-form-item
        v-show="!partnerForm.id"
        label="账号"
        :label-width="formLabelWidth"
        prop="account"
      >
        <el-input
          v-model="partnerForm.account"
          autocomplete="off"
          placeholder="请输入"
          maxlength="18"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        v-show="!partnerForm.id"
        label="密码"
        :label-width="formLabelWidth"
        prop="password"
      >
        <el-input
          v-model="partnerForm.password"
          autocomplete="off"
          placeholder="请输入"
          maxlength="20"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPartner, partnerDetail, updatePartner } from '@/api'
export default {
  props: {
    partnerVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formLabelWidth: '120px',
      min: 0,
      max: 100,
      partnerForm: {
        name: '',
        contact: '',
        mobile: '',
        ratio: 0,
        account: '',
        password: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        ratio: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.partnerForm.id ? '修改合作商' : '新增合作商'
    }
  },
  methods: {
    close() {
      this.$emit('closeFn')
      this.$refs.partnerRef.resetFields()
      this.partnerForm = {
        name: '',
        contact: '',
        mobile: '',
        ratio: 0,
        account: '',
        password: ''
      }
    },
    submit() {
      this.$refs.partnerRef.validate(async(valid) => {
        if (!valid) return
        if (this.partnerForm.id) {
          await updatePartner(this.partnerForm.id, this.partnerForm)
        } else {
          try {
            await addPartner(this.partnerForm)
          } catch (err) {
            this.$message.error('已存在此名称的对象')
            // return
          }
        }
        this.$message.success(this.title + '成功')
        this.close()
        this.$emit('addPartnerFn')
        // console.log(res)
      })
    },
    handleChange(value) {
      console.log(value)
    },
    async updataOpenFn(id) {
      console.log(id)
      const res = await partnerDetail(id)
      // console.log(res)
      this.partnerForm = res.data
    }
  }
}

</script>
<style lang='scss' scoped>
::v-deep .el-dialog__header {
  background: #fff;
  .el-dialog__title {
    font-weight: 700;
    font-size: 16px;
    color: #333;
  }
}
::v-deep .el-icon-close:before {
  /* color: #fff; */
  color: #909399;
}
</style>
